<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Register Src="~/Views/Menu/MainMenu.ascx" TagName="MainMenu" TagPrefix="mmuc" %>
<%@ Register Src="~/Views/Shared/LogOnUserControl.ascx" TagName="LogOnUserControl" TagPrefix="louc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <louc:LogOnUserControl ID="LogOnUserControl1" runat="server" />
     <!-- BEGIN: MainMenu -->    
    <mmuc:MainMenu ID="MainMenu1" runat="server" />
    <!-- END: MainMenu -->
    <br />
    <h2>
        Documents of Community</h2>
    <%
        //if (Session["CommunityId"] == null)
        //{
        //    return RedirectToAction("Index", "SelectCommunity", new { returnUrl = Request.RawUrl });
        //}

        int communityId = Convert.ToInt32(ViewData["CommunityId"]);

        MVCWeb.Models.HOACRMDataContext dc = new MVCWeb.Models.HOACRMDataContext();

        IList<MVCWeb.Models.DocCategory> allCategories = dc.DocCategories.ToList<MVCWeb.Models.DocCategory>();
        IList<MVCWeb.Models.DocCategory> parentCategories = allCategories.Where(c => c.ParentDocCategoryId == 0 || c.DocCategoryId == 0).ToList();
        IList<MVCWeb.Models.DocCategory> childCategories = allCategories.Where(c => c.ParentDocCategoryId > 0).ToList<MVCWeb.Models.DocCategory>();
        IList<MVCWeb.Models.Doc> allDocs = dc.Docs.Where(d => (dc.CommunityDocs.Where(cd => cd.CommunityId == communityId).Select(cd => cd.DocId).Contains((Int32?)(d.DocId)))).ToList<MVCWeb.Models.Doc>();
    %>
    <fieldset>
        <legend>Document categories </legend>
        <% foreach (var item in parentCategories)
           { %>
        <p>
            <b>
                <%= Html.ActionLink(Html.Encode(item.Category), "List", new { communityId = Request.QueryString["CommunityId"], categoryId = item.DocCategoryId })%></b>
            <%--(<%= Html.Encode(item.Docs.Count) %>)--%>
        </p>
        <% } %>
    </fieldset>
    <% if (Context.User.IsInRole("Administrators"))
       { %>
    <% foreach (var item in parentCategories)
       {
    %>
    <fieldset>
        <legend>
            <%= item.DocCategoryId == -1 ? "Documents have not been assigned to any category" : item.Category%>
        </legend>
        <br />
        <table id="document">
            <tr>
                <th style="width: 100%">
                    Title
                </th>
                <th style="width: 1%; white-space: nowrap">
                    Modified Date
                </th>
                <th style="width: 1%; white-space: nowrap">
                    Size (Kb)
                </th>
                <th style="width: 1%; white-space: nowrap">
                </th>
            </tr>
            <% foreach (var doc in allDocs.Where(d => d.DocCategoryId == item.DocCategoryId))
               { %>
            <tr>
                <td>
                    <%= Html.Encode(doc.Name)%>
                </td>
                <td>
                    <%= Html.Encode(doc.ModifiedDate.Value.ToShortDateString())%>
                </td>
                <td>
                    <%= MVCWeb.Helpers.FileHelper.GetFileSize(doc.Size) %>
                </td>
                <td>
                    <a target="_blank" href='<%= doc.URLLink %>'>Download</a>
                </td>
            </tr>
            <% } %>
            <% foreach (var subCat in childCategories.Where(c => c.ParentDocCategoryId == item.DocCategoryId))
               { %>
            <tr>
                <td colspan="4">
                    <br />
                    <b>
                        <%= subCat.Category %></b>
                </td>
            </tr>
            <% foreach (var doc in allDocs.Where(d => d.DocCategoryId == subCat.DocCategoryId))
               { %>
            <tr>
                <td>
                    <%= Html.Encode(doc.Name)%>
                </td>
                <td>
                    <%= Html.Encode(doc.ModifiedDate.Value.ToShortDateString())%>
                </td>
                <td>
                    <%= MVCWeb.Helpers.FileHelper.GetFileSize(doc.Size) %>
                </td>
                <td>
                    <a target="_blank" href='<%= doc.URLLink %>'>Download</a>
                </td>
            </tr>
            <% } %>
            <% } %>
        </table>
    </fieldset>
    <% } %>
    <% } %>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>
